package com.sparrow.source.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sparrow.source.common.dto.Result;
import com.sparrow.source.common.enums.ResultEnum;
import com.sparrow.source.common.utils.MinioUtils;
import com.sparrow.source.persistense.IasFace;
import com.sparrow.source.persistense.IasUser;
import com.sparrow.source.persistense.mapper.IasFaceMapper;
import com.sparrow.source.service.IIasFaceService;
import com.sparrow.source.service.IIasUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

import static com.sparrow.source.common.CommonConstant.MINIO_FACE_BUCKET;

/**
 * @author bryceyung
 * @since 2024/11/20
 */
@Service
@Slf4j
public class IasFaceServiceImpl extends ServiceImpl<IasFaceMapper, IasFace> implements IIasFaceService {

    @Autowired
    private IIasUserService userService;

    @Autowired
    private MinioUtils minioUtils;

    /**
     * 添加人脸照片
     *
     * @param userId
     * @param files
     * @return
     */
    @Override
    public Result addFaceImage(Integer userId, MultipartFile[] files) {
        IasUser user = userService.getById(userId);
        if (user == null) {
            return null;
        }
        Integer succCount = 0;
        List<IasFace> faceList = Lists.newArrayList();
        for (MultipartFile file : files) {
            try {
                String imagePath = minioUtils.uploadFileMinio(file, MINIO_FACE_BUCKET);
                succCount++;
                //保存人像信息
                IasFace face = new IasFace();
                face.setUserId(userId);
                face.setCreateTime(new Date());
                face.setImagePath(imagePath);
                face.setName(imagePath.substring(imagePath.lastIndexOf("/") + 1));
                user.setFaceImg(imagePath);
                faceList.add(face);
            } catch (Exception e) {
                log.error("addFaceImageError :{},{}", succCount, e.getMessage());
            }
        }
        String message = "成功上传 " + succCount + " 张人脸数据";
        baseMapper.batchInsert(faceList);
        userService.updateById(user);
        return Result.build(ResultEnum.SUCCESS).setObject(baseMapper.selectByUserId(userId)).setMessage(message);
    }

    @Override
    public Result updateFaceImage(Integer faceId, MultipartFile[] files) {
        IasFace face = baseMapper.selectById(faceId);
        if (face == null) {
            return Result.build(ResultEnum.NOT_ACCEPTABLE).setMessage("未查询到对应人脸信息");
        }
        face.setDeleted(true);
        //TODO 删除minio照片
        baseMapper.updateById(face);
        return addFaceImage(face.getUserId(), files);
    }

    @Override
    public List<IasFace> queryByUserId(Integer userId) {
        return baseMapper.selectByUserId(userId);
    }

    @Override
    public Boolean delete(Integer faceId) {
        IasFace face = baseMapper.selectById(faceId);
        if (face == null) {
            return false;
        }
        face.setDeleted(true);
        return baseMapper.updateById(face) > 0;
    }
}
